/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.google.gson.Gson;
import conexion.JsonFormat;
import conexion.SqlFormat;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.json.JsonArray;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import libreriaTicket.dao.DaoComuna;
import libreriaTicket.dao.DaoRecinto;
import libreriaTicket.dao.DaoRegion;
import libreriaTicket.entity.Comuna;
import libreriaTicket.entity.Region;
import libreriaTicket.entity.Recinto;

/**
 *
 * @author CITT-2013
 */
public class Recint extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            
        } finally {            
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        //---Se recupera la accion
       /* int accion = Integer.parseInt(request.getParameter("accion"));*/
        
        /*response.setContentType("application/json");
        PrintWriter out = response.getWriter();*/
        
        int accion  = Integer.parseInt(request.getParameter("accion"));
        
        if(accion == 10)
        {
            getComunas(request, response);
        }
        
        if(accion == SqlFormat.INSERTAR)
        {
            insertarRecinto(request, response);
        }
        if(accion == SqlFormat.SELECCIONAR)
        {
            getRecinto(request, response);
        }
        if(accion == SqlFormat.CONSULTAR)
        {
            getBuscar(request, response);
        }
        
        //---En caso de que se requiera las comunas por region
        
        
        
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public void getComunas(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        
        //--Se recuperan las comuans por region
       ArrayList<Comuna> comunas =  DaoComuna.sqlComunasByRegion(
                new Region(Integer.parseInt(request.getParameter("idRegion")))
                );
        
        //--se devuelve el json
        Gson json = new Gson();
        
        out.write(json.toJson(comunas));
        
        
    }
    
    public void insertarRecinto(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        
        //--Se recuperan las comuans por region
        Recinto recinto = new Recinto();
        recinto.setNombre(request.getParameter("nombre"));
        recinto.setCalle(request.getParameter("calle"));
        recinto.setDescripcion(request.getParameter("descripcion"));
        recinto.setCantidadPersona(Integer.parseInt(request.getParameter("capacidad")));
        recinto.setNumeroCalle(Integer.parseInt(request.getParameter("numero")));
        recinto.setComuna(new Comuna(Integer.parseInt(request.getParameter("comuna"))));
         
        DaoRecinto.sqlInsert(recinto);
        Gson json = new Gson();
        
    }
    
    public void getRecinto(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        
        //--Se recuperan las comuans por region
      ArrayList<Recinto> recinto = DaoRecinto.sqlSelectAll();
        
        //--se devuelve el json
        Gson json = new Gson();
        
        out.write(json.toJson(recinto));
        
        
    }
    
    public void getBuscar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        
        //--Se recuperan las comuans por region
        Recinto recinto2 = new Recinto();
        recinto2.setId(Integer.parseInt(request.getParameter("idRecinto")));

        Recinto recinto = DaoRecinto.sqlSelect(recinto2);
        Comuna comuna = DaoComuna.sqlSelect(new Comuna(recinto.getComuna().getIdComuna()));
        
        //Region region = DaoRegion.sqlSelect(new Region(comuna.getRegion().getIdRegion()));
        
        
        //--se devuelve el json
        Gson json = new Gson();
        
        out.write(json.toJson(comuna));
        
        
    }
    
   
}
